package org.example;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Arrays;

/**
 *  int read =  inputStream.read(); 实际上读取的是 byte值,返回的却是 int,做了转型
 */
public class AppIoStream2 {
    public static void main(String[] args) throws Exception {

        String fileName = "info.txt";
        String strContent = "我爱";

        String fileEncoding = "utf-8"; //[-26, -120, -111, -25, -120, -79]
//        String fileEncoding = "gbk"; // [-50, -46, -80, -82]
        byte[] bytes = strContent.getBytes(fileEncoding);
        String bytesString = Arrays.toString(bytes);
        System.out.println(bytesString);

        String path = AppIoStream2.class.getResource("").getPath();
        System.out.println(path);
        System.out.println(path.replace("file:","") );

        //对于jar包内的文件,不能使用FileInputStream,因为它不能读取jar包内的文件
//        FileInputStream fileInputStream = new FileInputStream(path.replace("file:","") + fileName);
//        int readInt = fileInputStream.read();


        /**
         * 对于jar包执行,读取jar包内的info.txt文件,得到的类是
         * sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream
         *
         * 对于解压后文件夹执行,读取info.txt文件,得到的类是
         * java.io.BufferedInputStream
         */
        InputStream inputStream = AppIoStream2.class.getResourceAsStream(fileName);
        System.out.println(inputStream.getClass().getName());

        BufferedReader bufferedReader = new BufferedReader(new java.io.InputStreamReader(inputStream,fileEncoding));

        String line = null;

        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }

        bufferedReader.close();
        inputStream.close();


//        int readInt = inputStream.read();
//        byte readByte = (byte) readInt;
//        System.out.println("readByte----readInt");
//        System.out.println(readByte+"----"+readInt);

    }
}
